package net.tngou.iask.util;

import java.io.File;
import java.io.IOException;
import java.net.ConnectException;

import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.io.FileUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
import org.apache.pdfbox.text.PDFTextStripper;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.document.DocumentFormat;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;



/**
 * PDF 转换
 * @author tngou@tngou.net
 *
 */
public class OpenOfficePdfConvert {

	private static String OFFICE_HOME ;//安装OPenOffice 的路径
	static{
		PropertiesConfiguration config = ConfigUtil.GetConfig("sys.properties");
		OFFICE_HOME=config.getString("openoffice_home");
	}
	
	/**
	 * PDF转换
	 * @param inputFile
	 * @return
	 */
	public static File  Office2Pdf(File inputFile) {
		DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration(); 
		config.setOfficeHome(OFFICE_HOME); 
		 OfficeManager officeManager = config.buildOfficeManager();  
		 officeManager.start(); 
		 OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
		 String filename =inputFile.getPath();	
		 File outputFile=new File(StringUtil.removeEnd(filename, _getExtensionName(filename))+"pdf");
		 converter.convert(inputFile, outputFile); 	
		 officeManager.stop();
		 return null;
		
	}
	
	
	/**
	 * 读取PDF文件
	 * @param pdfFile
	 * @return
	 */
	public static String Content(File pdfFile) {
		try {
			 PDDocument document= PDDocument.load(pdfFile);
			// 读文本内容
	         PDFTextStripper stripper=new PDFTextStripper();	       
	         return  stripper.getText(document);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";	
         
	}
	
    private static String _getExtensionName(String filename) {   
        if ((filename != null) && (filename.length() > 0)) {   
            int dot = filename.lastIndexOf('.');   
            if ((dot >-1) && (dot < (filename.length() - 1))) {   
                return filename.substring(dot + 1);   
            }   
        }   
        return filename;   
    }
    
    
	public static void main(String[] args) throws InvalidPasswordException, IOException {
//		 office2Pdf();
		File pdfFile =new File("C:\\Users\\Administrator\\Desktop\\基于影像大数据的肝癌多学科会诊平台.pdf");
		 PDDocument document = PDDocument.load(pdfFile);
		
         // 读文本内容
         PDFTextStripper stripper=new PDFTextStripper();      
         String content = stripper.getText(document);
         System.out.println("xxxxx"+content);
	}
}
